package com.gmcc.docmanager.loader;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import com.gmcc.docmanager.entity.Document;
import com.gmcc.docmanager.entity.DocumentFile;

public class DocDetailResponseParser extends ResponseParser {

	public DocDetailResponseParser(ResponseListener listener) {
		super(listener);
	}

	@Override
	public void doParse(String url, HttpResponse httpresp, InputStream is,
			String postData) {
		super.doParse(url, httpresp, is, postData);
		if (netResult && rootElement != null) {
			int errorCode = 1;
			errorCode = stringToInt(getNodeTextValue("errorCode"));
			Document document = null;
			if (errorCode != 0) {
				response(createFailureResponse(errorCode));
			} else {
				Node documentNode = super.getSingleNode("document");
				if (documentNode != null) {
					NamedNodeMap documentNodeMap = documentNode.getAttributes();
					document = new Document();
					document.setDocID(getAttributeValue(documentNodeMap,
							"docID"));
					for (Node documentChildNode = documentNode.getFirstChild(); documentChildNode != null; documentChildNode = documentChildNode
							.getNextSibling()) {
						if (documentChildNode.getNodeType() == Node.ELEMENT_NODE) {
							if ("title".equals(documentChildNode.getNodeName())) {
								document.setTitle(getNodeTextValue(documentChildNode));
							} else if ("createtime".equals(documentChildNode
									.getNodeName())) {
								documentChildNode.getNodeValue();
								document.setCreatetime(getNodeTextValue(documentChildNode));
							} else if ("mj".equals(documentChildNode
									.getNodeName())) {
								document.setMj(getNodeTextValue(documentChildNode));
							} else if ("bgqx".equals(documentChildNode
									.getNodeName())) {
								document.setBgqx(getNodeTextValue(documentChildNode));
							} else if ("zrz".equals(documentChildNode
									.getNodeName())) {
								document.setZrz(getNodeTextValue(documentChildNode));
							} else if ("gdrq".equals(documentChildNode
									.getNodeName())) {
								document.setGdrq(getNodeTextValue(documentChildNode));
							} else if ("wenhao".equals(documentChildNode
									.getNodeName())) {
								document.setWenhao(getNodeTextValue(documentChildNode));
							} else if ("keyword".equals(documentChildNode
									.getNodeName())) {
								document.setKeyword(getNodeTextValue(documentChildNode));
							} else if ("attachments".equals(documentChildNode
									.getNodeName())) {
								document.setDocumentFiles(new ArrayList<DocumentFile>());
								for (Node efileNode = documentChildNode
										.getFirstChild(); efileNode != null; efileNode = efileNode
										.getNextSibling()) {
									if (efileNode.getNodeType() == Node.ELEMENT_NODE
											&& "efile".equals(efileNode
													.getNodeName())) {
										DocumentFile documentFile = new DocumentFile();
										documentFile.setDocID(document
												.getDocID());
										NamedNodeMap efileNodeMap = efileNode
												.getAttributes();
										documentFile
												.setEfileID(getAttributeValue(
														efileNodeMap, "efileID"));
										documentFile
												.setThumbnail(getAttributeValue(
														efileNodeMap,
														"thumbnail"));
										documentFile
												.setDownloadUrl(getAttributeValue(
														efileNodeMap,
														"downloadUrl"));
										documentFile
												.setFileType(getAttributeValue(
														efileNodeMap, "doc"));
										for (Node efileChildNode = efileNode
												.getFirstChild(); efileChildNode != null; efileChildNode = efileChildNode
												.getNextSibling()) {
											if (efileChildNode.getNodeType() == Node.ELEMENT_NODE) {
												if ("filename"
														.equals(efileChildNode
																.getNodeName())) {
													documentFile
															.setFileName(getNodeTextValue(efileChildNode));
												} else if ("thumbnail"
														.equals(efileChildNode
																.getNodeName())) {
													documentFile
															.setThumbnail(getNodeTextValue(efileChildNode));
												} else if ("downloadURL"
														.equals(efileChildNode
																.getNodeName())) {
													documentFile
															.setDownloadUrl(getNodeTextValue(efileChildNode));
												}
											}
										}
										document.getDocumentFiles().add(
												documentFile);
									}
								}
							}
						}
					}
				}
			}

			Response response = new Response();
			response.setCode(errorCode);
			response.setResponseObject(document);
			response(response);
		}
	}
}
